﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Collections;
using KPIS.GERP.GHRMS.APP.Classes;
using KPIS.Utils;
using Telerik.Web.UI;
using KPIS.GERP.GHRMS.BLL;
using KPIS.GERP.GHRMS.IBLL;
using KPIS.GERP.GHRMS.MODEL;

namespace KPIS.GERP.GHRMS.APP.App_UI.UserControls
{
    public partial class PS_AbsenceTypes : System.Web.UI.UserControl
    {
        private IAbsenceTypeBLL abBLL = new AbsenceTypeBLL();

        private object _dataItem = null;
        protected void Page_Load(object sender, EventArgs e)
        {
            // txtCountDayAdvance.Attributes.Add("onkeypress", "return isNumberKey(event)");

            if (DataItem != null)
            {
                LoadApproveProcessList();
                if (!(DataItem is Telerik.Web.UI.GridInsertionObject))
                {
                    lblAbsenceTypeSeq.Text = DataBinder.Eval(DataItem, "Sequence").ToString();
                    txtAbsenceName.Text = Convert.ToString(DataBinder.Eval(DataItem, "Name")); 
                    txtRemarks.Text = DataBinder.Eval(DataItem, "Remarks").ToString();
                    try
                    {
                        cmbAppPrcSeq.SelectedValue = DataBinder.Eval(DataItem, "ApproveProcessSeq").ToString();
                    }catch
                    {
                        cmbAppPrcSeq.Text = "(ไม่พบข้อมูล:" + DataBinder.Eval(DataItem, "ApproveProcessSeq");
                    }
                }
                else
                {

                }
                btnUpdate.Visible = !(DataItem is Telerik.Web.UI.GridInsertionObject);
                btnAddnew.Visible = (DataItem is Telerik.Web.UI.GridInsertionObject);
            }
        }

        private void LoadApproveProcessList()
        {
            Dictionary<int, string> data = new Dictionary<int, string>(); 
            foreach(var d in abBLL.ListApproveProcess())
            {
                data.Add(d.Key, d.Value);
            }
            cmbAppPrcSeq.DataSource = data;
            cmbAppPrcSeq.DataBind();
        }

        public object DataItem
        {
            get
            {
                return this._dataItem;
            }
            set
            {
                this._dataItem = value;
            }
        }

        protected void btnAddnew_Click(object sender, EventArgs e)
        {
            abBLL.Insert(txtAbsenceName.Text.Trim()
                , new Main().ConvertNullInt(cmbAppPrcSeq.SelectedValue)
                , txtRemarks.Text.Trim()
                , 1);
         }

        protected void btnUpdate_Click(object sender, EventArgs e)
        {
            abBLL.Update(Int32.Parse(lblAbsenceTypeSeq.Text.Trim())
                , new Main().ConvertNullInt(cmbAppPrcSeq.SelectedValue)
                , txtAbsenceName.Text.Trim() 
                , txtRemarks.Text.Trim()
                , 1);
        }

        protected void btnCancel_Click(object sender, EventArgs e)
        {

        }
    }
}